<?php
namespace app\wx\controller;
use think\Controller;
use think\Request;
class Wx_member extends Controller
{

	public $request;
	public function __construct(Request $request){
		parent::__construct();
		$this->request=$request;
	}

    public function oauth($backurl = '')
    {
        $backurl = $backurl == '' ? $backurl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] : $backurl;
        $scope   = "snsapi_userinfo"; //微信用户授权模式 snsapi_userinfo snsapi_base
        $backurl = urlencode($backurl);
        $get     = $this->request->get();
        if ($get['state'] == 'oauth') {
            return $this->openid($get['code']);
        }
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->config['appid']}&redirect_uri={$backurl}&response_type=code&scope={$scope}&state=oauth#wechat_redirect";
        header("location:" . $url);
        exit();
    }

    public function openid($code)
    {
        $url    = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->config['appid']}&secret={$this->config['appsecret']}&code={$code}&grant_type=authorization_code";
        $result = $this->http_get($url);
        return $this->userinfo($result['openid']);
    }

    public function userinfo($openid)
    {
        $url    = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$this->access_token}&openid={$openid}&lang=zh_CN";
        $result = $this->http_get($url);
        return $result;
    }
}
